package com.wwy.monitor.util;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.buc.sso.client.util.SimpleUserUtil;
import com.alibaba.buc.sso.client.vo.BucSSOUser;
import com.autonavi.dyui.common.exception.BizException;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by tangyaohua on 2017/7/11.
 */
public class UserUtils {
    static public BucSSOUser getUser(HttpServletRequest request) throws IOException, ServletException {
        return SimpleUserUtil.getBucSSOUser(request);
    }

    static public String getBucUserName(HttpServletRequest request) throws IOException, ServletException {
        BucSSOUser user = SimpleUserUtil.getBucSSOUser(request);
        String userName;
        if (user == null) {
            throw new BizException(-1, "ç¨æ·æªç»å½");
        }
        if (user.getNickNameCn() != null && !"".equals(user.getNickNameCn())) {
            userName = user.getNickNameCn();
        } else if (user.getLoginName() != null && !"".equals(user.getLoginName())) {
            userName = user.getLoginName();
        } else {
            userName = user.getEmailAddr();
        }
        return userName;
    }

    static public String getBucEmpId(HttpServletRequest request) throws IOException, ServletException {
        BucSSOUser user = SimpleUserUtil.getBucSSOUser(request);
        return user != null ? user.getEmpId() : "";
    }

    static public String getBucUser(HttpServletRequest request) throws IOException, ServletException {
        BucSSOUser user = SimpleUserUtil.getBucSSOUser(request);
        return user != null ? String.format("%s(%s)", user.getLastName(), user.getEmpId()) : "";
    }

    /**
     * å»æå·¥å·åé¢ç0
     *
     * @param empId
     * @return
     */
    public static String empIdFilter(String empId) {
        String result = empId.replaceAll("^(0+)", "");
        return StringUtils.isNotEmpty(result) ? result : "";
    }
}